//
//  PhotoHelpr.m
//  Unity-iPhone
//
//  Created by 墨羽天城 on 16/2/3.
//
//

#import "PhotoHelpr.h"

@implementation PhotoHelpr

static PhotoHelpr * _instance = nil;

+(PhotoHelpr *)instance
{
    if (_instance == nil)
    {
        _instance = [[PhotoHelpr alloc] init];
    }
    return _instance;
}
//开始拍照
-(void)takePhoto:(NSString *)path
{
    self.savePath = path;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        UIViewController* controller = UnityGetGLViewController();
        [controller presentViewController:picker animated:YES completion:nil];
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

//打开本地相册
-(void)LocalPhoto:(NSString *)path
{
    self.savePath = path;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    UIViewController* controller = UnityGetGLViewController();
    [controller presentViewController:picker animated:YES completion:nil];

}

//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    NSString * fileNam = @"/image.png";
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        image = [self compressImage:image];
        
        NSData *data;
        data = UIImageJPEGRepresentation(image, 0.5f);
        
        
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createFileAtPath:[self.savePath stringByAppendingString:fileNam] contents:data attributes:nil];
        
        
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:nil];
        float f = [[fileManager attributesOfItemAtPath:[self.savePath stringByAppendingString:fileNam] error:nil] fileSize]/(1024);
        
        UnitySendMessage("PluginHelper", "LoadPhoto", [fileNam UTF8String]);
        //创建一个选择后图片的小图标放在下方
        //类似微薄选择图后的效果
        //UIImageView *smallimage = [[[UIImageView alloc] initWithFrame:
        //                            CGRectMake(50, 120, 40, 40)] autorelease];
        
        //smallimage.image = image;
        //加在视图中
    }
    else{
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"您取消了选择图片");
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//压缩图片
- (UIImage *)compressImage:(UIImage *)imgSrc
{
    CGSize size = {200, 200};
    UIGraphicsBeginImageContext(size);
    CGRect rect = {{0,0}, size};
    [imgSrc drawInRect:rect];
    UIImage *compressedImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return compressedImg;
}

@end
